﻿package {
	import com.sohu.sns.avatar.BinaryTransfer;
	import com.sohu.sns.avatar.EditWindow;
	import com.sohu.sns.avatar.InfoSprite;
	import com.sohu.sns.avatar.PhotoLoader;
	import com.sohu.sns.avatar.PhotoThumb;
	import com.sohu.sns.avatar.ProgressBar;
	import com.sohu.sns.avatar.ThumbButton;

	import flash.display.BitmapData;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.external.ExternalInterface;
	import flash.ui.ContextMenu;

	public class ThumbEditor extends MovieClip {
		private var savingBtn : Sprite;
		private var SlideBtn : Class;
		private var photoServer : String;
		private var ReEditBtn : Class;
		private var leftIcon : Sprite;
		
		private var smallEditWin : EditWindow;
		private var smallThumb : PhotoThumb;
		private var smallThumbButton : ThumbButton;
		
		private var MainLoading : Class;
		private var reEditDisBtn : Sprite;
		private var cameraSnapSaveUrl : String;
		private var loading : InfoSprite;
		private var reEditBtn : Sprite;
		private var LeftIcon : Class;
		private var mainLoading : Sprite;
		private var ReEditDisBtn : Class;
		private var picSource : PhotoLoader;
		private var slideBarMid : Sprite;
		private var thumbnailWin : Sprite;
		private var SnappDisBtn : Class;
		private var stageWidth : Number = 514;
		private var previewBtn : Sprite;
		private var snappDisBtn : Sprite;
		private var SaveBtn : Class;
		private var slideBarBg : Sprite;
		private var resetBtn : Sprite;
		private var PreviewBtn : Class;
		private var RightIcon : Class;
		private var snappBtn : Sprite;
		private var saveBtn : Sprite;
		private var slideBar : ProgressBar;
		private var SlideBarBg : Class;
		private var previewState : Sprite;
		private var ResetBtn : Class;
		private var editState : Sprite;
		private var saveUrl : String;
		private var rightIcon : Sprite;
		private var SnappBtn : Class;
		private var postUrl : String;
		private var stageHeight : Number = 464;
		private var RestBtn : Class;
		private var editData : BitmapData;
		private var BackBtn : Class;
		private var photoUrl : String;
		private var SavingBtn : Class;
		private var slideBtn : Sprite;
		private var photoId : String;
		private var restBtn : Sprite;
		private var iconSaveUrl : String;

		public function ThumbEditor() {
			// 初始化显示部分
			SlideBtn = AvatarEditor_SlideBtn;
			SlideBarBg = AvatarEditor_SlideBarBg;
			PreviewBtn = AvatarEditor_PreviewBtn;
			BackBtn = AvatarEditor_BackBtn;
			SnappBtn = AvatarEditor_SnappBtn;
			SnappDisBtn = AvatarEditor_SnappDisBtn;
			ReEditBtn = AvatarEditor_ReEditBtn;
			ReEditDisBtn = AvatarEditor_ReEditDisBtn;
			ResetBtn = AvatarEditor_ResetBtn;
			SaveBtn = AvatarEditor_SaveBtn;
			LeftIcon = AvatarEditor_LeftIcon;
			RightIcon = AvatarEditor_RightIcon;
			RestBtn = AvatarEditor_RestBtn;
			SavingBtn = AvatarEditor_SavingBtn;
			MainLoading = AvatarEditor_MainLoading;
			editState = new Sprite();
			previewState = new Sprite();
			loading = new InfoSprite();
			thumbnailWin = new Sprite();
			addChild(this.loading);
			this.mainLoading = new MainLoading();
			addChild(this.mainLoading);
			this.mainLoading.x = (this.stageWidth - this.mainLoading.width) / 2;
			this.mainLoading.y = (this.stageHeight - this.mainLoading.height) / 2;
			// 初始化其它组件
			this.drawBg();
			this.init();
		}

		/**
		 * 绘制背景
		 */
		private function drawBg() : void {
			this.graphics.beginFill(0xCFDEE9);
			this.graphics.drawRect(0, 413, 514, 53);
			this.graphics.endFill();
		}

		/**
		 * 初始化
		 */
		private function init() : void {
			this.removeMenu();
			stage.align = StageAlign.TOP_LEFT;
			stage.scaleMode = StageScaleMode.NO_SCALE;
			// 外部传入图片相关参数
			this.photoUrl = loaderInfo.parameters["photoUrl"];
			this.postUrl = loaderInfo.parameters["postUrl"];
			this.saveUrl = loaderInfo.parameters["saveUrl"];
			var photoId : String = loaderInfo.parameters["photoId"];
//			this.photoUrl = "avatar_origin/1332609763.jpg";
//			this.postUrl = loaderInfo.parameters["postUrl"];
//			this.saveUrl = loaderInfo.parameters["saveUrl"];
//			var photoId : String = "1332609763";
			this.iconSaveUrl = this.saveUrl;
			this.cameraSnapSaveUrl = this.postUrl;
			if (photoId && photoId.length > 0) {
				this.photoId = photoId;
			} else {
				this.photoId = "";
			}
			// 初始化状态相关现时对象
			addChild(this.editState);
			addChild(this.previewState);
			// 切换图片或者摄像头模式
			this.initUI();
			this.picSource = new PhotoLoader(this.photoUrl);
			this.picSource.getEditSource();
			this.picSource.addEventListener(PhotoLoader.COMPLETE, onEditData);
			this.photoServer = this.photoUrl;
		}

		/**
		 * 去除多余的右键菜单选项
		 */
		private function removeMenu() : void {
			var menu : ContextMenu = new ContextMenu();
			menu.hideBuiltInItems();
			this.contextMenu = menu;
		}

		private function onSeek(event : Event) : void {
			this.smallEditWin.setPhotoScale(this.slideBar.rate);
		}

		private function editSmall(event : Event = null) : void {
			this.smallThumbButton.active();
			this.smallEditWin.visible = true;
			if (this.smallEditWin.scaleRange < 0.1) {
				this.slideBar.disable();
				this.slideBar.alpha = 0;
			} else {
				this.slideBar.enable();
				this.slideBar.setRate(this.smallEditWin.scalePercent);
				this.slideBar.alpha = 1;
			}
		}

		private function initListeners() : void {
			this.previewBtn.addEventListener(MouseEvent.CLICK, previewAvatar);
			this.reEditBtn.addEventListener(MouseEvent.CLICK, reEdit);
			this.saveBtn.addEventListener(MouseEvent.CLICK, saveAvatar);
			this.slideBar.addEventListener(ProgressBar.BAR_SEEK, onSeek);
			this.slideBar.addEventListener(ProgressBar.SEEKING, onSeek);
			this.slideBar.addEventListener(ProgressBar.SEEK_END, onSeekEnd);
			this.restBtn.addEventListener(MouseEvent.CLICK, onReset);
			this.smallThumbButton.addEventListener(ThumbButton.CLICKED, editSmall);
			this.smallEditWin.addEventListener(EditWindow.SNAP, onSmallSnap);
		}

		private function saveAvatar(event : MouseEvent) : void {
			this.saveBtn.visible = false;
			this.savingBtn.visible = true;
			this.reEditBtn.visible = false;
			this.reEditDisBtn.visible = true;
			// 原来是大的保存完成后保存小的，现在跳过
			saveSmall();
		}

		private function previewAvatar(event : MouseEvent) : void {
			this.saveView();
		}

		private var smallWidth : int = 177;//138;
		private var smallHeight : int = 105;//30;

		private function editPhoto() : void {
			// 开始编辑
			this.editView();
			// 初始化小图预览
			this.editState.addChild(this.smallEditWin);
			this.smallEditWin.edit(this.editData, smallWidth, smallHeight);
			this.smallEditWin.setPhotoScale(1);
			// 默认编辑小图
			this.editSmall();
		}

		private function onSmallSnap(event : Event) : void {
			this.smallThumbButton.drawData(this.smallEditWin.thumbBmpData);
		}

		private function onSeekEnd(event : Event) : void {
			this.smallEditWin.setSnapp();
		}

		private function onSaveError(event : Event) : void {
			var msgStr : String = (event.target as BinaryTransfer).msg;
			this.reEdit(null);
			ExternalInterface.call("avatarError", msgStr);
		}

		private function saveSmall() : void {
			var binaryTransfer : BinaryTransfer = new BinaryTransfer(this.iconSaveUrl, "big", this.photoServer, "icon", this.photoId);
			binaryTransfer.addEventListener(BinaryTransfer.COMPLETE, onSmallSaved);
			binaryTransfer.addEventListener(BinaryTransfer.ERROR, onSaveError);
			binaryTransfer.transferData(this.smallEditWin.thumbBmpData);
		}

		private function initUI() : void {
			this.createEditWin();
			this.slideBtn = new SlideBtn();
			this.slideBarMid = new SlideBarBg();
			this.slideBarBg = new SlideBarBg();
			this.leftIcon = new LeftIcon();
			this.rightIcon = new RightIcon();
			this.slideBar = new ProgressBar(this.slideBtn, this.slideBarMid, this.slideBarBg, 7, this.leftIcon, this.rightIcon);
			this.editState.addChild(this.slideBar);
			this.thumbnailWin.visible = true;
			this.editState.addChild(this.thumbnailWin);
			this.slideBar.x = 36;
			this.slideBar.y = 373;
			this.previewBtn = new PreviewBtn();
			this.snappBtn = new SnappBtn();
			this.snappDisBtn = new SnappDisBtn();
			this.reEditBtn = new ReEditBtn();
			this.reEditDisBtn = new ReEditDisBtn();
			this.saveBtn = new SaveBtn();
			this.savingBtn = new SavingBtn();
			this.resetBtn = new ResetBtn();
			this.restBtn = new RestBtn();
			this.editState.addChild(this.previewBtn);
			this.editState.addChild(this.restBtn);
			this.previewState.addChild(this.reEditDisBtn);
			this.previewState.addChild(this.reEditBtn);
			this.previewState.addChild(this.saveBtn);
			this.previewState.addChild(this.savingBtn);
			this.savingBtn.mouseChildren = false;
			this.reEditDisBtn.mouseChildren = false;
			this.previewState.graphics.lineStyle(1, 0xD2DDE5);
			this.previewState.graphics.moveTo(15, 55);
			this.previewState.graphics.lineTo(498, 55);
			this.snappBtn.x = (this.stageWidth - this.snappBtn.width) / 2;
			this.snappBtn.y = 428;
			this.snappDisBtn.x = this.snappBtn.x;
			this.snappDisBtn.y = this.snappBtn.y;
			this.previewBtn.x = (this.stageWidth - this.previewBtn.width) / 2;
			this.previewBtn.y = 428;
			this.restBtn.x = 276;
			this.restBtn.y = 373;
			this.reEditBtn.x = 256;
			this.reEditBtn.y = 428;
			this.reEditDisBtn.x = this.reEditBtn.x;
			this.reEditDisBtn.y = this.reEditBtn.y;
			this.reEditDisBtn.visible = false;
			this.saveBtn.x = 178;
			this.saveBtn.y = 428;
			this.savingBtn.x = this.saveBtn.x;
			this.savingBtn.y = this.saveBtn.y;
			this.smallThumb = new PhotoThumb(smallWidth, smallHeight, "缩略图");
			this.previewState.addChild(this.smallThumb);
			this.smallThumbButton = new ThumbButton(smallWidth, smallHeight, "缩略图预览");
			this.smallThumbButton.drawData(new BitmapData(smallWidth, smallHeight, false, 13421772));
			this.thumbnailWin.addChild(this.smallThumbButton);
			this.thumbnailWin.x = 514 - this.thumbnailWin.width - 12;
			this.thumbnailWin.y = 30;
			this.smallThumb.x = 73;
			this.smallThumb.y = 85;
			this.initListeners();
		}

		private function onReset(event : MouseEvent) : void {
			this.slideBar.setRate(1);
			this.smallEditWin.reset();
		}

		private function saveView() : void {
			this.reEditBtn.visible = true;
			this.reEditDisBtn.visible = false;
			this.editState.visible = false;
			this.previewState.visible = true;
			this.saveBtn.visible = true;
			this.savingBtn.visible = false;
			this.smallThumb.draw(this.smallEditWin.thumbBmpData);
		}

		private function onEditData(event : Event) : void {
			this.editData = event.target['editData'] as BitmapData;
			this.editPhoto();
			this.removeChild(this.mainLoading);
		}

		private function editView() : void {
			this.editState.visible = true;
			this.previewState.visible = false;
			this.loading.hide();
		}

		private function reEdit(event : MouseEvent) : void {
			this.editView();
		}

		private function createEditWin() : void {
			this.smallEditWin = new EditWindow(null, 280, 330, smallWidth, smallHeight);
			this.smallEditWin.x = 20;
			this.smallEditWin.y = 32;
		}

		private function onSmallSaved(event : Event) : void {
			var msgStr : String = (event.target as BinaryTransfer).msg;
			ExternalInterface.call("avatarSaved", msgStr);
		}
	}
}
